import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @author selrain
 * @description 解决方案是否过期切面
 * @date 2017-11-14 17:35:53
 */

@Aspect
public class MerchantSolutionInfoAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(MerchantSolutionInfoAspect.class);

    @Autowired
    private MarchSolutionExportService marchSolutionExportService;

    @Autowired
    private MerchantSettingService merchantSettingService;

    @Around("@annotation(com.weimob.saas.ec.customer.annotation.MerchantSolutionInfoAnnotation)")
    public Object CheckMerchantSolutionInfoPermission(ProceedingJoinPoint point) throws Throwable {

        MerchantIdRequestVo requestVo = new MerchantIdRequestVo();
        //店铺信息查询入参
        BaseMerchantSettingInputVo baseMerchantSettingInputVo = new BaseMerchantSettingInputVo();
        //获取请求入参
        Object[] args = point.getArgs();
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof BaseRequest) {
                BaseRequest httpRequestVo = (BaseRequest) args[i];
                requestVo.setPid(httpRequestVo.getPid());

                baseMerchantSettingInputVo.setPid(httpRequestVo.getPid());
                baseMerchantSettingInputVo.setStoreId(httpRequestVo.getStoreId());

            }
        }
        SoaResponse<ExportSolutionInfo, Void> soaResponse = null;
        try {
            soaResponse = marchSolutionExportService.getMerchantBindSolutionInfo(requestVo);
        } catch (Exception e) {
            LOGGER.error("getMerchantBindSolutionInfo" + ",globalTicket: " + (soaResponse != null ? soaResponse.getGlobalTicket() : "null"), e);
        }
        //是否过期
        Boolean isExpired = false;
        // 0-正常返回 300100100004-店铺不存在返回code
        if (soaResponse != null && ("0".equals(soaResponse.getReturnCode()) || "300100100004".equals(soaResponse.getReturnCode()))) {
            ExportSolutionInfo exportSolutionInfo = soaResponse.getResponseVo();
            //已过期
            if (exportSolutionInfo == null) {
                isExpired = true;
            } else {
                Boolean packFree = exportSolutionInfo.getPackFree();
                //非免费套餐
                if (packFree != null && !packFree) {
                    Date startTime = exportSolutionInfo.getStartTime();
                    Date endTime = exportSolutionInfo.getEndTime();
                    Date date = new Date();
                    //套餐是否过期
                    if (date.before(startTime) || date.after(endTime)) {
                        isExpired = true;
                    }
                }
            }
        }
        if (soaResponse != null && "300100100004".equals(soaResponse.getReturnCode())) {
            isExpired = true;
        }
        if (isExpired) {
            HttpResponse<StoreInfo> httpResponse = new HttpResponse<>();
            httpResponse.setData(getStoreInfo(baseMerchantSettingInputVo));
            httpResponse.setErrcode(NavigationErrorCode.TRANSFER_SOLUTION_ERROR.getCode());// 错误码
            httpResponse.setErrmsg(NavigationErrorCode.TRANSFER_SOLUTION_ERROR.getErrMsg());// 返回内容
            httpResponse.setGlobalTicket(soaResponse.getGlobalTicket());
            httpResponse.setMonitorTrackId(soaResponse.getMonitorTrackId());
            return httpResponse;
        }
        return point.proceed();
    }

    private StoreInfo getStoreInfo(BaseMerchantSettingInputVo baseMerchantSettingInputVo) {
        StoreInfo storeInfo = new StoreInfo();
        SoaResponse<StoreInfoVo, MerchantSettingCommonErrorVo> merchantSoaResponse = null;
        try {
            merchantSoaResponse = merchantSettingService.getStoreInfo(baseMerchantSettingInputVo);
        } catch (Exception e) {
            LOGGER.error("getMerchantAndStoreInfo" + ",globalTicket: " + (merchantSoaResponse != null ? merchantSoaResponse.getGlobalTicket() : "null"), e);
        }
        if (merchantSoaResponse != null) {
            StoreInfoVo storeInfoVo = merchantSoaResponse.getResponseVo();
            if (storeInfoVo != null) {
                storeInfo.setStoreName(storeInfoVo.getStoreName());
            }
        }
        return storeInfo;
    }
}
